package srzy.module

import scala.collection.immutable.Range
import srzy.common.Logging
import srzy.common.Config
import scala.collection.mutable.ListBuffer

object CdrComp extends AbsCaseDeal with Logging {

  //  logger.debug("typeToCdrMap:" + typeToCdrMap)

  def getIntList(s: String, cc: Option[(String, List[Int])]) = {
    val len = srzy.common.split(s, """\|""").length
    cc match {
      case Some(c) => if (s == "") {
        List.empty[Int]
      } else {
        logger.debug("len=" + len)

        val intl = c._1 match {
          case "+" => {
            List(0 until len: _*).filter(c._2.indexOf(_) != -1)
          }
          case "-" => {
            List(0 until len: _*).filter(c._2.indexOf(_) == -1)
          }
          case "*" => {
            List(0 until len: _*)
          }
        }
        logger.debug("intList=" + intl)
        intl
      }
      case None => List(0 until len: _*)
    }
  }

  def getIntList(len: Int, cc: Option[(String, List[Int])]) = {
    cc match {
      case Some(c) =>
        logger.debug("len=" + len)
        val intl = c._1 match {
          case "+" => {
            List(0 until len: _*).filter(c._2.indexOf(_) != -1)
          }
          case "-" => {
            List(0 until len: _*).filter(c._2.indexOf(_) == -1)
          }
          case "*" => {
            List(0 until len: _*)
          }
        }
        logger.debug("intList=" + intl)
        intl
      case None => List(0 until len: _*)
    }
  }

  def cutStr(s: String, c: List[Int]) = {

    val arrStr = srzy.common.split(s, """\|""")
    logger.debug("arrStr=" + arrStr)

    c.map(i => {
      val s = arrStr.apply(i)
      logger.debug(i + "=" + s)
      if (s.indexOf(";") == -1) {
        s
      } else {
        srzy.common.split(s, (";")).sortWith(_ > _).mkString(";")
      }
    }).mkString("|")
  }

  def cutStr2(s: String, c: List[Int]) = {

    val arrStr = srzy.common.split(s, """\|""")
    logger.debug("arrStr=" + arrStr)

    arrStr.zipWithIndex.filter(e => c.contains(e._2)).map(_._1).map(i => {
      //      val s = arrStr.apply(i)
      //      logger.debug(i + "=" + s)
      if (s.indexOf(";") == -1) {
        s
      } else {
        srzy.common.split(s, (";")).sortWith(_ > _).mkString(";")
      }
    }).mkString("|")
  }

  def cdrCompare() = {

  }

  def deal = {

    if (getCaseInfo.stdFullCdrs.length + getCaseInfo.realFullCdrs.length > 0) {

      //      val cdrField = typeToCdrMap.apply("")
      //      val cdrDir = cdrDirMap.apply(caseType)
      val sessionId = getCaseInfo.sessionId

      //      logger.debug("CaseType:" + caseType)
      //      logger.debug("cdrField:" + cdrField)
      //      logger.debug("cdrDir:" + cdrDir)
      logger.debug("sessionId:" + sessionId)

      if (sessionId != "") {
        //        val (legalStdFull, illegalStdFull) = getCaseInfo.stdFullCdrs.partition(_._2.contains(sessionId))
        //        if (illegalStdFull.length != 0) {
        //          logger.error("cdr is not cotain this case")
        //          logger.error("illegalStdFull:" + illegalStdFull.mkString("\n"))
        //          getCaseInfo.stdFullCdrs = legalStdFull
        //        }

        val (legalRealFull, illegalRealFull) = getCaseInfo.realFullCdrs.partition(_._2.contains(sessionId))
        if (illegalRealFull.length != 0) {
          logger.error("cdr is not cotain this case")
          logger.error("illegalRealFull:" + illegalRealFull.mkString("\n"))
          getCaseInfo.realFullCdrs = legalRealFull
        }
      }

      if (getCaseInfo.stdFullCdrs.length != getCaseInfo.realFullCdrs.length) {
        logger.debug("cdrInfo.stdFullCdrs length not equal to cdrInfo.realFullCdrs length")
        getCaseInfo.errLog += "cdrInfo.stdFullCdrs length not equal to cdrInfo.realFullCdrs length" + "\n"
      } else if (getCaseInfo.stdFullCdrs.map(_._1) != getCaseInfo.realFullCdrs.map(_._1)) {
        logger.debug("cdrInfo.stdFullCdrs type not equal to cdrInfo.realFullCdrs type")
        getCaseInfo.errLog += "cdrInfo.stdFullCdrs type not equal to cdrInfo.realFullCdrs type" + "\n"
      } else {
        //        val res:List[(String, String)]
        var stdCuttedCdrs0 = ListBuffer.empty[(String, String)]
        var realCuttedCdrs0 = ListBuffer.empty[(String, String)]
        (0 until getCaseInfo.stdFullCdrs.size).toList.map(i => {
          //getCaseInfo.stdFullCdrs.apply(i).map(x =>  getIntList(x._2, typeToCdrMap.get(x._1)))))
          val stdFullCdr = getCaseInfo.stdFullCdrs.apply(i)
          val realFullCdr = getCaseInfo.realFullCdrs.apply(i)
          val minLen = Math.min(srzy.common.split(stdFullCdr._2, """\|""").length,
            srzy.common.split(realFullCdr._2, """\|""").length)
          val intList = getIntList(minLen, typeToCdrMap.get(realFullCdr._1))

          stdCuttedCdrs0.append((stdFullCdr._1, cutStr(stdFullCdr._2, intList)))
          realCuttedCdrs0.append((realFullCdr._1, cutStr(realFullCdr._2, intList)))
        })

        //        getCaseInfo.stdCuttedCdrs = getCaseInfo.stdFullCdrs
        //          .map(x => (x._1, cutStr(x._2, getIntList(x._2, typeToCdrMap.get(x._1)))))
        //        getCaseInfo.realCuttedCdrs = getCaseInfo.realFullCdrs
        //          .map(x => (x._1, cutStr(x._2, getIntList(x._2, typeToCdrMap.get(x._1)))))

        getCaseInfo.stdCuttedCdrs = stdCuttedCdrs0.toList
        getCaseInfo.realCuttedCdrs = realCuttedCdrs0.toList

        val res = (0 until getCaseInfo.stdCuttedCdrs.size).toList.map(i => {
          val stdCutted = getCaseInfo.stdCuttedCdrs.apply(i)._2
          val realCutted = getCaseInfo.realCuttedCdrs.apply(i)._2

          val stdCuttedList = srzy.common.split(stdCutted, """\|""")
          val realCuttedList = srzy.common.split(realCutted, """\|""")

          if (stdCuttedList.length == realCuttedList.length) {
            val l = (0 until stdCuttedList.length).toList.map(j => {
              //              logger.debug("stdCutted :" + j + " " + stdCuttedList.apply(j) +
              //                "\nrealCutted:" + j + " " + realCuttedList.apply(j))
              if (stdCuttedList.apply(j) == realCuttedList.apply(j)) {
                (true, "")
              } else {
                //                logger.debug("stdCutted :" + j + " " + stdCuttedList.apply(j) +
                //                  "\nrealCutted:" + j + " " + realCuttedList.apply(j))
                (false, "stdCutted :" + j + " " + stdCuttedList.apply(j) +
                  "\nrealCutted:" + j + " " + realCuttedList.apply(j))
              }
            }).filterNot(_._1)
            //            logger.debug("l:" + l)
            if (l.length == 0) {
              (true, "")
            } else {
              val h = l.head
              (h._1, "diff num:" + l.length + "\n" + h._2)
            }
          } else {
            logger.debug("the cutted cdr length is not the same , stdCutted:" + stdCuttedList.length +
              " , realCutted:" + realCuttedList.length)
            (false, "the cutted cdr length is not the same , stdCutted:" + stdCuttedList.length +
              " , realCutted:" + realCuttedList.length)
            //            getCaseInfo.errLog += "the cdr length is not the same" + "\n"
          }
        })
        //        logger.debug("res:" + res)
        val errRes = res.filter(_._1 == false)
        if (errRes.length != 0) {
          getCaseInfo.errLog += errRes.map(_._2).mkString("\n")
        } else {
          getCaseInfo.cdrCompResult = true
        }
      }
    } else {
      getCaseInfo.cdrCompResult = true
    }

    logger.debug("stdCuttedCdr:\n" + getCaseInfo.stdCuttedCdrs.mkString("\n"))
    logger.debug("stdFullCdr:\n" + getCaseInfo.stdFullCdrs.mkString("\n"))
    logger.debug("realCuttedCdr:\n" + getCaseInfo.realCuttedCdrs.mkString("\n"))
    logger.debug("realFullCdr:\n" + getCaseInfo.realFullCdrs.mkString("\n"))

  }

}
